package com.aiton.bamin.bamindaijiadrier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiton.administrator.shane_library.shane.utils.ClickUtils;
import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.aiton.administrator.shane_library.shane.utils.Installation;
import com.aiton.administrator.shane_library.shane.utils.IsMobileNOorPassword;
import com.aiton.administrator.shane_library.shane.widget.SingleBtnDialog;
import com.aiton.bamin.bamindaijiadrier.R;
import com.aiton.bamin.bamindaijiadrier.constant.Constant;
import com.aiton.bamin.bamindaijiadrier.model.User;
import com.blankj.utilcode.utils.EncryptUtils;
import com.blankj.utilcode.utils.ToastUtils;
import com.igexin.sdk.PushManager;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import cz.msebera.android.httpclient.Header;

public class ApplyPregressActivity extends ZjbBaseActivity implements View.OnClickListener {

    private LinearLayout mLine_Check_Progress;
    private LinearLayout mLine_Show_Progress;
    private EditText mEdt_password;
    private EditText mEdt_phone;
    private TextView mTv_progress;
    private ImageView mImg_progress_register;
    private ImageView mImg_progress_info;
    private ImageView mImg_progress_mianshi;
    private ImageView mImg_progress_lukao;
    private ImageView mImg_progress_peixun;
    private ImageView mImg_progress_success;
    private User mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_pregress);
        init();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews() {
        if (mUser != null) {
            Log.e("ApplyPregressActivity", "用户是否为空------》" + mUser.getMes());
            showLoadingDialog();
            mLine_Show_Progress.setVisibility(View.VISIBLE);
            mLine_Check_Progress.setVisibility(View.GONE);
            String auditingStatus = mUser.getDatas().getAuditingStatus();
            checkUserStatus(auditingStatus);
        }
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageView_back).setOnClickListener(this);
        findViewById(R.id.btn_check).setOnClickListener(this);

    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        mUser = (User) intent.getSerializableExtra(Constant.INTENT_KEY.USER);

    }

    @Override
    protected void findID() {
        mLine_Check_Progress = (LinearLayout) findViewById(R.id.line_editInfo);
        mLine_Show_Progress = (LinearLayout) findViewById(R.id.line_show_progress);

        mEdt_password = (EditText) findViewById(R.id.edt_password);
        mEdt_phone = (EditText) findViewById(R.id.edt_phone);

        mTv_progress = (TextView) findViewById(R.id.tv_progress);
        mImg_progress_register = (ImageView) findViewById(R.id.img_progress_register);
        mImg_progress_info = (ImageView) findViewById(R.id.img_progress_info);
        mImg_progress_mianshi = (ImageView) findViewById(R.id.img_progress_mianshi);
        mImg_progress_lukao = (ImageView) findViewById(R.id.img_progress_lukao);
        mImg_progress_peixun = (ImageView) findViewById(R.id.img_progress_peixun);
        mImg_progress_success = (ImageView) findViewById(R.id.img_progress_success);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_back:
                finishTo();
                break;
            case R.id.btn_check:
                showLoadingDialog();

                if (!IsMobileNOorPassword.isMobileNO(mEdt_phone.getText().toString().trim())) {
                    mEdt_phone.setError("请输入正确的手机号码");
                    cancelLoadingDialog();

                } else if (TextUtils.isEmpty(mEdt_password.getText().toString().trim())) {
                    mEdt_password.setError("密码不能为空");
                    cancelLoadingDialog();

                } else if(!ClickUtils.isFastDoubleClick(1500)){
                    // TODO: 2016/9/9 请求服务器
                    checkProgress();
                }else{
                    cancelLoadingDialog();
                }

                break;
        }
    }

    private void checkProgress() {
        String password = mEdt_password.getText().toString().trim();
        String password4Encrypt = EncryptUtils.encryptSHA256ToString(password);
        String url = Constant.Url.PASSWORD_LOGIN;
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        //每次存储唯一标识
        final String DeviceId = Installation.id(this);
        params.put("phone", mEdt_phone.getText().toString().trim());
        params.put("password", password4Encrypt);
        params.put("deviceid", DeviceId + "");
        params.put("key", PushManager.getInstance().getClientid(this));
        asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String s = new String(responseBody);
                Log.e("onResponse ", "登录返回结果" + s);

                try {
                    final User user = GsonUtils.parseJSON(s, User.class);
                    if (user.getStatus() == 0) {
                        //存储手机号和用户id到本地
                        String auditingStatus = user.getDatas().getAuditingStatus();
                        checkUserStatus(auditingStatus);
                        mLine_Check_Progress.setVisibility(View.GONE);
                        mLine_Show_Progress.setVisibility(View.VISIBLE);
                    } else {
                        ToastUtils.showShortToast(ApplyPregressActivity.this,user.getMes());
                        cancelLoadingDialog();
                    }
                } catch (Exception e) {
                    cancelLoadingDialog();
                    showFailDialog("解析出错");

                    ToastUtils.showShortToast(ApplyPregressActivity.this,"解析出错");

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                ToastUtils.showShortToast(ApplyPregressActivity.this,"登录超时……");

                showFailDialog("登录超时");
                cancelLoadingDialog();
                Log.e("LoginActivity", "密码登录失败");

            }
        });
    }
    private void showFailDialog(String title) {
        final SingleBtnDialog singleBtnDialog = new SingleBtnDialog(this, title, "确定");
        singleBtnDialog.setClicklistener(new SingleBtnDialog.ClickListenerInterface() {
            @Override
            public void doWhat() {
                finishTo();
                singleBtnDialog.dismiss();
            }
        });
        singleBtnDialog.show();
    }

    /**
     * 检查用户状态
     *
     * @param auditingStatus
     */
    private void checkUserStatus(String auditingStatus) {
        cancelLoadingDialog();
        mTv_progress.setText(auditingStatus);

        switch (auditingStatus) {
            case "合格通过":
//                mTv_progress.setText("合格通过");
                mImg_progress_register.setImageResource(R.mipmap.progress_success);
                mImg_progress_info.setImageResource(R.mipmap.progress_success);
                mImg_progress_mianshi.setImageResource(R.mipmap.progress_success);
                mImg_progress_lukao.setImageResource(R.mipmap.progress_success);
                mImg_progress_peixun.setImageResource(R.mipmap.progress_success);
                mImg_progress_success.setImageResource(R.mipmap.progress_success);
                break;
            case "等待图片上传":
            case "图片审核失败":
//                mTv_progress.setText("等待图片上传");
                mImg_progress_register.setImageResource(R.mipmap.progress_success);
                mImg_progress_info.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_mianshi.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_lukao.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_peixun.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_success.setImageResource(R.mipmap.progress_unfinish);
                break;
            case "已上传图片":
            case "等待面试":
            case "面试未通过":
            case "等待安排笔试":
            case "笔试不合格":
//                mTv_progress.setText("等待面试笔试");
                mImg_progress_register.setImageResource(R.mipmap.progress_success);
                mImg_progress_info.setImageResource(R.mipmap.progress_success);
                mImg_progress_mianshi.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_lukao.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_peixun.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_success.setImageResource(R.mipmap.progress_unfinish);
                break;
            case "等待路考":
            case "路考未通过":
//                mTv_progress.setText("等待路考");
                mImg_progress_register.setImageResource(R.mipmap.progress_success);
                mImg_progress_info.setImageResource(R.mipmap.progress_success);
                mImg_progress_mianshi.setImageResource(R.mipmap.progress_success);
                mImg_progress_lukao.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_peixun.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_success.setImageResource(R.mipmap.progress_unfinish);
                break;
            case "等待安排培训":
            case "培训中":
//                mTv_progress.setText("等待安排培训");
                mImg_progress_register.setImageResource(R.mipmap.progress_success);
                mImg_progress_info.setImageResource(R.mipmap.progress_success);
                mImg_progress_mianshi.setImageResource(R.mipmap.progress_success);
                mImg_progress_lukao.setImageResource(R.mipmap.progress_success);
                mImg_progress_peixun.setImageResource(R.mipmap.progress_unfinish);
                mImg_progress_success.setImageResource(R.mipmap.progress_unfinish);
                break;
            default:
                mImg_progress_register.setImageResource(R.mipmap.progress_success);
                mImg_progress_info.setImageResource(R.mipmap.progress_success);
                mImg_progress_mianshi.setImageResource(R.mipmap.progress_success);
                mImg_progress_lukao.setImageResource(R.mipmap.progress_success);
                mImg_progress_peixun.setImageResource(R.mipmap.progress_success);
                mImg_progress_success.setImageResource(R.mipmap.progress_unfinish);
        }
    }
}
